package com.ems.servlet;

import com.ems.dao.UserDAO;
import com.ems.model.User;
import com.ems.util.PasswordUtil;
import java.io.IOException;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userId = request.getParameter("userId");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");
        String role = request.getParameter("role");

        HttpSession session = request.getSession();
        User currentUser = (User) session.getAttribute("user");

        // 只有管理员可以注册新管理员
        if (role.equals("admin") && (currentUser == null || !currentUser.getRole().equals("admin"))) {
            request.setAttribute("error", "只有管理员可以注册新管理员");
            request.getRequestDispatcher("register.jsp").forward(request, response);
            return;
        }

        if (!password.equals(confirmPassword)) {
            request.setAttribute("error", "两次输入的密码不一致");
            request.getRequestDispatcher("register.jsp").forward(request, response);
            return;
        }

        try {
            UserDAO userDAO = new UserDAO();
            if (userDAO.getUserById(userId) != null) {
                request.setAttribute("error", "用户ID已存在");
                request.getRequestDispatcher("register.jsp").forward(request, response);
                return;
            }

            String hashedPassword = PasswordUtil.hashPassword(password);
            User newUser = new User(userId, username, hashedPassword, role);

            if (userDAO.addUser(newUser)) {
                if (currentUser != null && currentUser.getRole().equals("admin")) {
//                    response.sendRedirect("home.jsp");
                    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/home.jsp");
                    dispatcher.forward(request, response);
                } else {
                    response.sendRedirect("login.jsp");
                }
            } else {
                request.setAttribute("error", "注册失败，请稍后再试");
                request.getRequestDispatcher("register.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", "注册失败，请稍后再试");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       // response.sendRedirect("register.jsp");
        RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/register.jsp");
        dispatcher.forward(request, response);
    }
}